<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Sms_m extends CI_Model{
	public $user_id = '';
	public $user_name = '';
	function __construct(){//重载父类的数据库连接方法
		parent::__construct();
		$this->load->helper('string');
		$this->user_id = $this->session->userdata('user_id');
		$this->user_name = $this->session->userdata('user_name');
	}
	function checknew(){
		$query = $this->db->where('u_id',$this->user_id)->get('sms_check');
		return $query->num_rows();
		/*
		$array = array('to_id' => $this->user_id, 'sms_read' => '0');
		$this->db->where($array);
		$query = $this->db->get('sms');
		return $query->num_rows();
		*/
	}
	function showSms(){
		$this->db->where('u_id',$this->user_id)->delete('sms_check');
		
		$this->db->select('*');
		$this->db->from('sms');
 		$this->db->join('sms_body', 'sms_body.bodyid = sms.bodyid');
		$names = array('0', '4');
 		$this->db->where_in('sms_delete', $names);
		$arr = array('to_id' => $this->user_id,'sms_read'=>'');//改为已读
		$this->db->where($arr); 
		$this->db->order_by("sms_body.sendtime", "desc"); 
		$query = $this->db->get();
		//echo $this->db->last_query();//输出随后一次查询的语

		$data = array('sms_read' => '1','readtime' => date("Y-m-d H:i:s",time()));//改为已读
		$this->db->where('sms_read', '0');
		$this->db->where('to_id', $this->user_id);
		$this->db->update('sms', $data);

		return $query->result_array();
	}


	function getinboxlistdata($data){
		$this->db->from('sms');
		$this->db->join('sms_body', 'sms_body.bodyid = sms.bodyid');
		$this->db->where('sms.to_id', $this->user_id); 

		//if(@$data['title'] != '' || @$data['sendname'] != '' || @$data['eids'] != '' || @$data['eide'] != ''){
		if(array_key_exists("title",$data))
			$this->db->like('email_body.title', $data['title']); 
		if(array_key_exists("sendname",$data))
			$this->db->where_in('email_body.sender',explode(",",reduce_multiples($data['sendname'],',',true)));
		if(array_key_exists("eids",$data))
			$this->db->where('email_body.sendertime >=', $data['eids']);
		if(array_key_exists("eide",$data)){
			$eide = date('Y-m-d',strtotime($this->input->post('eide')." + 1 day"));
			$this->db->where('email_body.sendertime <=', $eide);
		}
		//}
		$this->db->where_in('sms.sms_delete', array('0', '4'));
		
		//在order、group或limit前查询总数
        $db = clone($this->db);
        $total = $this->db->count_all_results();
		
		$this->db = $db;
		$this->db->order_by('sms_body.'.$data['sort'], $data['order']); 
		$start=($data['page']-1)*$data['rows'];
		$this->db->limit($data['rows'],$start);
		$query = $this->db->get();
		//echo $this->db->last_query();//输出随后一次查询的语
		$data = $query->result_array();
		return array('data'=>$data, 'total'=>$total);
	}
	/*
	function setread($id){
		$data = array(
				   'sms_read' => '0',
				   'readtime' => ''
				);
		$this->db->where_in('smsid', $id);
		$this->db->update('sms', $data); 
		//echo $this->db->last_query();//输出随后一次查询的语
		return true;
	}
	*/
	/*收件箱删除邮件方法*/
	/*首先更新delete字段，0->1,4->2*/
	/*从smsid找到bodyid*/
	/*检查对应的bodyid字段是否全为2*/
	/*如果是则删除邮件主体和收件人信息*/
	function inboxdel($id){
		$this->db->trans_begin();//开始事务处理
		foreach($id as $smsid){
			$isSuccess = $this->db->query("update `yj_sms` set `sms_delete`= case `sms_delete` when '0' then '1' when '4' then '2' end where `smsid` = '".$smsid."'");
			if($isSuccess){
				$query = $this->db->query("select * from `yj_sms` where `bodyid` = (select `bodyid` from `yj_sms` where `smsid` = '".$smsid."') and `sms_delete` <> '2'");
				if($query->num_rows() == 0){
					$row = $this->db->query("select `bodyid` from `yj_sms` where `smsid` = '".$smsid."'")->first_row();
					$tables = array('sms', 'sms_body');
					$this->db->where('bodyid', $row->bodyid);
					$this->db->delete($tables);	
				}
			}
		}
		if($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();//事件回滚	
			return false;		
		}else{
			$this->db->trans_commit();//提交成功
			return true;
		}//事务结束
	}

	
/*--已发送---------------------------------------------*/
	function getsentboxlistdata($data){
		$this->db->from('sms_body');
		$this->db->join('sms', 'sms_body.bodyid = sms.bodyid');
		$this->db->where('sms_body.sender', $this->user_name); 

		//if(@$data['title'] != '' || @$data['sendname'] != '' || @$data['eids'] != '' || @$data['eide'] != ''){
		if(array_key_exists("title",$data))
			$this->db->like('email_body.title', $data['title']); 
		if(array_key_exists("sendname",$data))
			$this->db->where_in('email_body.sender',explode(",",reduce_multiples($data['sendname'],',',true)));
		if(array_key_exists("eids",$data))
			$this->db->where('email_body.sendertime >=', $data['eids']);
		if(array_key_exists("eide",$data)){
			$eide = date('Y-m-d',strtotime($this->input->post('eide')." + 1 day"));
			$this->db->where('email_body.sendertime <=', $eide);
		}
		//}
		$this->db->where_in('sms.sms_delete', array('0', '1'));
		//$this->db->group_by('sms_body.bodyid'); 
		
		//在order、group或limit前查询总数
        $db = clone($this->db);
        $total = $this->db->count_all_results();
		
		$this->db = $db;
		$this->db->order_by('sms_body.'.$data['sort'], $data['order']); 
		$start=($data['page']-1)*$data['rows'];
		$this->db->limit($data['rows'],$start);
		$query = $this->db->get();
		//echo $this->db->last_query();//输出随后一次查询的语
		$data = $query->result_array();
		return array('data'=>$data, 'total'=>$total);
	}
	
	/*发件箱删除邮件方法*/
	/*首先更新delete字段，0->1,4->2*/
	/*从smsid找到bodyid*/
	/*检查对应的bodyid字段是否全为2*/
	/*如果是则删除邮件主体和收件人信息*/
	function sentboxdel($id){//发件箱删除邮件方法
		$this->db->trans_begin();//开始事务处理
		foreach($id as $bodyid){
			$isSuccess = $this->db->query("update `yj_sms` set `sms_delete`= case `sms_delete` when '0' then '4' when '1' then '2' end where `bodyid` = '".$bodyid."'");
			if($isSuccess){
				$query = $this->db->query("select * from `yj_sms` where `bodyid` = '".$bodyid."' and `sms_delete` <> '2'");
				if($query->num_rows() == 0){
					$tables = array('sms', 'sms_body');
					$this->db->where('bodyid', $bodyid);
					$this->db->delete($tables);	
				}
			}
		}
		if($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();//事件回滚	
			return false;		
		}else{
			$this->db->trans_commit();//提交成功
			return true;
		}//事务结束
	}


	function smsaddpost($data){//添加新短信数据到数据库
		$sms_user_idarr = explode(",",reduce_multiples($data['p_sms_id'],',',true));
		$sms_user_namearr = explode(",",reduce_multiples($data['p_sms_name'],',',true));
		$this->db->trans_begin();//开始事务处理
			//邮件短信提醒数据主体插入到数据库			
			$smsarr = array(
                'bodyid' => '' ,
                'sender' => $this->user_name,
                'smstype' => '1',
				'alerttype' => $data['p_sms_type_alert'],
				'content' => $this->user_name.'向您发送的短信：'.$data['p_sms_content'],
				'sendtime' => date("Y-m-d H:i:s",time()),
				'remindurl' => 'sms/inbox/'
             ); 
			$this->db->insert('sms_body', $smsarr); //插入短信提醒主体数据
			$bodyid = $this->db->insert_id();
			
			//$addressee = explode(",",substr($data['p_sj_user'],0,-1));
			for($i = 0; $i<count($sms_user_idarr);$i++){
				$arr[0][] = array(//短信收信人数据
					'smsid' => '',
					'to_id' => $sms_user_idarr[$i],
					'to_name' => $sms_user_namearr[$i],
					'sms_read' => '0',
					'readtime' => date("Y-m-d H:i:s",time()),
					'sms_delete' => '0',
					'bodyid' => $bodyid
				);
				$arr[1][] = array(//用于检索的数据表数据
					'id' => '',
					'u_id' => $sms_user_idarr[$i],
					'u_name' => $sms_user_namearr[$i],
					'add_date' => time(),
					'm_id' => '1_'.$bodyid
				);
			}
			$this->db->insert_batch('sms',$arr[0]); //插入接收邮件数组
			$this->db->insert_batch('sms_check',$arr[1]); //插入接收邮件数组
		
		if($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();//事件回滚	
			return false;		
		}else{
			$this->db->trans_commit();//提交成功
			return true;
		}//事务结束
	}

}
?>